package com.htfyun.modem.utils;

import java.lang.reflect.Method;

public class SystemPropertyUtil {

    private final static String TAG = "SystemPropertyUtil";
    private static Object instance = null;

    static {
        try {
            Class<?> c = Class.forName("hteink.DeviceManager");
            Method m = c.getDeclaredMethod("getInstance");
            instance = m.invoke(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String get(String propName) {
/*        String line = "";
        try {

            Process process = Runtime.getRuntime().exec("getprop " + propName);
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            BufferedReader input = new BufferedReader(ir);
            line = input.readLine();
            ir.close();
            input.close();
            if (line == null || line.length() == 0) {
                line = "";
            }
        } catch (Exception ex) {
            Log.e(TAG, "Unable to read sysprop " + propName);
            return "";
        }
        return line;*/
        return get(propName, "");
    }

    public static String get(String propName, String defaultValue) {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");

            Object object = ReflectUtils.invokeMethod(instance, "getProperty", propName);
            if (object instanceof String) {
                String value = (String) object;
                if (!isEmpty(value)) {
                    return (String) object;
                }
            }
        } catch (Exception e) {
            DebugLog.e(e.getMessage());
            return defaultValue;
        }
        return defaultValue;
    }

    public static boolean getBoolean(String propName, boolean defaultValue) {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");

            Object object = ReflectUtils.invokeMethod(instance, "getProperty", propName);
            if (object instanceof String) {
                String result = (String) object;
                if ("1".equals(result) || "true".equalsIgnoreCase(result)) {
                    return true;
                } else if ("0".equals(result) || "false".equalsIgnoreCase(result)) {
                    return false;
                }
            }
        } catch (Exception e) {
            DebugLog.e(e.getMessage());
            return defaultValue;
        }
        return defaultValue;
    }


    public static void set(String propName, String value) {

        boolean setOK = false;
        int tryCount = 5;
        while (!setOK && (tryCount-- > 0)) {
            setOK = doSetSystemProperty(propName, value);
        }
    }

    private static boolean doSetSystemProperty(String propName, String value) {

/*        try {
            Runtime.getRuntime().exec("setprop " + propName + " " + value);
            String readBackString = get(propName);
            if (TextUtils.isEmpty(readBackString)) {
                return false;
            }
            if (readBackString.equals(value)) {
                return true;
            }
        } catch (Exception ex) {
            Log.e(TAG, "Unable to set sysprop " + propName);
            return false;
        }
        return false;*/
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");
            ReflectUtils.invokeMethod(instance, "setProperty", propName, value);
            return true;
        } catch (Exception e) {
            DebugLog.e(e.toString());
            return false;
        }
    }

    public static void setWifiAddr(String wifiAddr) {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");
            ReflectUtils.invokeMethod(instance, "setWifiAddr", wifiAddr);
        } catch (Exception e) {
            DebugLog.e(e.toString());
        }
    }

    public static void setBtAddr(String btAddr) {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");
            ReflectUtils.invokeMethod(instance, "setBtAddr", btAddr);
        } catch (Exception e) {
            DebugLog.e(e.toString());
        }
    }

    public static boolean setCountryCode(String countryCode) {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");
            return (Boolean) ReflectUtils.invokeMethod(instance, "setCountryCode", countryCode);
        } catch (Exception e) {
            DebugLog.e(e.toString());
            return false;
        }
    }

    public static String getCountryCode() {
        try {
//            Class<?> c = Class.forName("hteink.DeviceManager");
//            Method m = c.getDeclaredMethod("getInstance");
//            Object instance = m.invoke(null);
            return (String) ReflectUtils.invokeMethod(instance, "getCountryCode");
        } catch (Exception e) {
            return "";
        }
    }

    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }

}
